function onLineTest() {
    if (navigator.onLine) {
        //正常工作
        console.log("online");
    } else {
        //执行离线状态时的任务
        console.log("offline");
    }
}

EventUtil.addHandler(window, "online", function () {
    console.log("Now Online");
});
EventUtil.addHandler(window, "offline", function () {
    console.log("Now Offline");
});

//应用缓存
//Cookie
function cookieTest() {
    document.cookie = "name=Nicholas";
    document.cookie = encodeURIComponent("name") + "=" +
        encodeURIComponent("Nicholas");
    document.cookie = encodeURIComponent("name") + "=" +
        encodeURIComponent("Nicholas") + "; domain=.wrox.com; path=/";

    //设置cookie
    CookieUtil.set("name", "Nicholas");
    CookieUtil.set("book", "Professional JavaScript");
    //读取cookie 的值
    console.log(CookieUtil.get("name")); //"Nicholas"
    console.log(CookieUtil.get("book")); //"Professional JavaScript"
    console.log(document.cookie);
    //删除cookie
    CookieUtil.unset("name");
    CookieUtil.unset("book");

    //设置cookie，包括它的路径、域、失效日期
    CookieUtil.set("name", "Nicholas", "/books/projs/", "www.wrox.com",
        new Date("January 1, 2018"));
    //删除刚刚设置的cookie
    CookieUtil.unset("name", "/books/projs/", "www.wrox.com");
    //设置安全的cookie
    CookieUtil.set("name", "Nicholas", null, null, null, true);
}

//子cookie
function subCookieTest() {
    document.cookie = "data=name=Nicholas&book=Professional%20JavaScript";
    //取得全部子cookie
    var data = SubCookieUtil.getAll("data");
    console.log(data.name); //"Nicholas"
    console.log(data.book); //"Professional JavaScript"
    //逐个获取子cookie
    console.log(SubCookieUtil.get("data", "name")); //"Nicholas"
    console.log(SubCookieUtil.get("data", "book")); //"Professional JavaScript"
}

// 如果你担心开发中可能会达到单域名的cookie 上限，那么子cookie 可是一个非常有吸引力的备选方
// 案。不过，你需要更加密切关注cookie 的长度，以防超过单个cookie 的长度限制。
function setSubCookieTest() {
    // document.cookie = "data=name=Nicholas&book=Professional%20JavaScript";
    //设置两个cookie
    SubCookieUtil.set("data", "name", "Nicholas");
    SubCookieUtil.set("data", "book", "Professional JavaScript");
    SubCookieUtil.set("data2", "name", "Bobby");
    SubCookieUtil.set("data2", "book", "Bobby's Book");
    console.log(document.cookie);
    //设置全部子cookie 和失效日期
    // debugger;
    SubCookieUtil.setAll("data", {name: "Gubojun", book: "Android Book"},
        new Date("June 1, 2018"));
    console.log(document.cookie);
    //修改名字的值，并修改cookie 的失效日期
    SubCookieUtil.set("data", "name", "Bobby", new Date("June 11, 2018"));
    console.log(document.cookie);
    //仅删除名为name 的子cookie
    SubCookieUtil.unset("data", "name");
    console.log(document.cookie);
    //删除整个cookie
    SubCookieUtil.unsetAll("data");
    console.log(document.cookie);
}

//Web存储机制
function sessionStorageTest() {
    //使用方法存储数据
    sessionStorage.setItem("name", "Nicholas");
    //使用属性存储数据
    sessionStorage.book = "Professional JavaScript";
    //只适用于IE8
    // sessionStorage.begin();
    // sessionStorage.name = "Nicholas";
    // sessionStorage.book = "Professional JavaScript";
    // sessionStorage.commit();
    //使用方法读取数据
    var name = sessionStorage.getItem("name");
    console.log(name);
    //使用属性读取数据
    var book = sessionStorage.book;
    console.log(book);
    console.log("---------------------------------------------------------");
    for (var i = 0, len = sessionStorage.length; i < len; i++) {
        var key = sessionStorage.key(i);
        var value = sessionStorage.getItem(key);
        console.log(key + "=" + value);
    }
    // for (var key in sessionStorage){
    //     var value = sessionStorage.getItem(key);
    //     console.log(key + "=" + value);
    // }
    //使用delete 删除一个值——在WebKit 中无效
    delete sessionStorage.name;
    //使用方法删除一个值
    sessionStorage.removeItem("book");
}

EventUtil.addHandler(document, "storage", function (event) {
    alert("Storage changed for " + event.domain);
});

function localStorageTest() {
    //使用方法存储数据
    localStorage.setItem("name", "Nicholas");
    //使用属性存储数据
    localStorage.book = "Professional JavaScript";
    //使用方法读取数据
    var name = localStorage.getItem("name");
    //使用属性读取数据
    var book = localStorage.book;
    console.log(name);
    console.log(book);
}

//为了兼容只支持globalStorage 的浏览器，可以使用以下函数
function getLocalStorage() {
    if (typeof localStorage == "object") {
        return localStorage;
    } else if (typeof globalStorage == "object") {
        return globalStorage[location.host];
    } else {
        throw new Error("Local storage not available.");
    }
}

function indexedDBTest() {
//  IDBDatabaseException.UNKNOWN_ERR(1)：意外错误，无法归类。
//  IDBDatabaseException.NON_TRANSIENT_ERR(2)：操作不合法。
//  IDBDatabaseException.NOT_FOUND_ERR(3)：未发现要操作的数据库。
//  IDBDatabaseException.CONSTRAINT_ERR(4)：违反了数据库约束。
//  IDBDatabaseException.DATA_ERR(5)：提供给事务的数据不能满足要求。
//  IDBDatabaseException.NOT_ALLOWED_ERR(6)：操作不合法。
//  IDBDatabaseException.TRANSACTION_INACTIVE_ERR(7)：试图重用已完成的事务。
//  IDBDatabaseException.ABORT_ERR(8)：请求中断，未成功。
//  IDBDatabaseException.READ_ONLY_ERR(9)：试图在只读模式下写入或修改数据。
//  IDBDatabaseException.TIMEOUT_ERR(10)：在有效时间内未完成操作。
//  IDBDatabaseException.QUOTA_ERR(11)：磁盘空间不足。
    var indexedDB = window.indexedDB || window.msIndexedDB || window.mozIndexedDB ||
        window.webkitIndexedDB;

    var request, database;
    request = indexedDB.open("admin");
    request.onerror = function (event) {
        alert("Something bad happened while trying to open: " +
            event.target.errorCode);
    };
    request.onsuccess = function (event) {
        database = event.target.result;
        console.log("open success ");
        console.log(database);
        dataBaseSetVersion(request, database);
        var user = {
            username: "007",
            firstName: "James",
            lastName: "Bond",
            password: "foo"
        };
        var db=database;
        // var store = db.createObjectStore("users", { keyPath: "username" });
        var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
        var transaction = db.transaction("users", IDBTransaction.READ_WRITE);
        var request = db.transaction("users").objectStore("users").get("007");
        request.onerror = function(event){
            alert("Did not get the object!");
        };
        request.onsuccess = function(event){
            var result = event.target.result;
            alert(result.firstName); //"James"
        };
    };
}

function dataBaseSetVersion(request, database) {
    if (database.version != 1) {
        request = database.setVersion("1.0");
        request.onerror = function (event) {
            alert("Something bad happened while trying to set version: " +
                event.target.errorCode);
        };
        request.onsuccess = function (event) {
            alert("Database initialization complete. Database name: " + database.name +
                ", Version: " + database.version);
        };
    } else {
        console.log("Database already initialized. Database name: " + database.name +
            ", Version: " + database.version);
    }
}

indexedDBTest();

